package com.ydcycloud.admin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smart.mvc.config.ConfigUtils;
import com.smart.mvc.model.Pagination;
import com.smart.mvc.model.Result;
import com.smart.mvc.provider.PasswordProvider;
import com.smart.sso.rpc.AuthenticationRpcService;
import com.smart.sso.rpc.Permission;
import com.smart.sso.rpc.Role;
import com.smart.sso.rpc.RolePermission;
import com.smart.sso.rpc.UserRole;
import com.smart.util.StringUtils;
import com.ydcycloud.admin.dao.ManagerDao;
import com.ydcycloud.admin.model.Manager;
import com.ydcycloud.admin.service.ManagerService;
import com.ydcycloud.sys.service.impl.BaseServiceImpl;

@Component("managerService")
public class ManagerServiceImpl extends BaseServiceImpl<ManagerDao, Manager, Integer> implements ManagerService {
	
	@Resource private AuthenticationRpcService authenticationRpcService;

	@Autowired
	public void setDao(ManagerDao dao) {
		this.dao = dao;
	}

	@Override
	public void updatePwd(Integer id, String newPwd) {
		
		if(id!=null && StringUtils.isNotBlank(newPwd)){
			
			Manager db=get(id);
			if(db!=null){
				boolean success=authenticationRpcService.updatePassword(SecurityUtils.getSubject().getPrincipal().toString(), newPwd);
				if(success){
					db.setPassword(PasswordProvider.encrypt(newPwd));
					save(db);
				}
			}
		}
	}

	@Override
	public Result listRole(Integer pageNo,Integer pageSize,String name,Integer sessionAdminId) {
		
		List<Role> list = authenticationRpcService.findRoleList(pageNo,pageSize,name,
				SecurityUtils.getSubject().getPrincipal().toString(), ConfigUtils.getProperty("app.code"));
		if(list!=null){
			for(int i = 0;i < list.size();i++){
				list.get(i).setIsEnableStr(list.get(i).getIsEnable()?"启用":"禁用");
			}
			Result r = Result.createSuccessResult();
			Pagination<Role> p = new Pagination<>(pageNo, pageSize);
			p.setList(list);
			p.setRowCount(list.size());
			r.setData(p);
			return r;
		}else
			return Result.createErrorResult();
	}

	@Override
	public void enable(Boolean isEnable, List<Integer> ajaxIds) {
		authenticationRpcService.enable(isEnable,ajaxIds);
	}

	@Override
	public Role getRole(Integer id) {
		return authenticationRpcService.getRole(id,SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
	}

	@Override
	public void saveOrUpdateRole(Role role) {
		authenticationRpcService.saveOrUpdateRole(role,SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
	}

	@Override
	public List<Permission> listPermission() {
		return authenticationRpcService.listPermision(SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
	}

	@Override
	public List<RolePermission> findByRoleId(Integer roleId) {
		return authenticationRpcService.findByRoleId(roleId,SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
	}

	@Override
	public Integer allocate(Integer roleId, List<RolePermission> list) {
		
		return authenticationRpcService.allocate(roleId,list,SecurityUtils.getSubject().getPrincipal().toString());
	}

	@Override
	public Pagination<com.smart.sso.rpc.Manager> findPaginationByAccount(String account,Integer pageNo,Integer pageSize) {
		
		List<com.smart.sso.rpc.Manager> list = authenticationRpcService.findUserByaccount(account,pageNo,pageSize,ConfigUtils.getProperty("app.code"));;
		Pagination<com.smart.sso.rpc.Manager> p = new Pagination<>(pageNo, pageSize);
		p.setList(list);
		return p;
	}

	@Override
	public com.smart.sso.rpc.Manager getManager(Integer id) {
		
		return authenticationRpcService.getManager(id);
	}

	@Override
	public void saveOrUpdateManager(com.smart.sso.rpc.Manager manager) {
		authenticationRpcService.saveOrUpdateManager(manager,SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
	}

	@Override
	public void enableManager(Boolean isEnable, List<Integer> ajaxIntegerIds) {
		authenticationRpcService.enableManager(isEnable,ajaxIntegerIds,SecurityUtils.getSubject().getPrincipal().toString());
	}

	@Override
	public Integer deleteManagerById(List<Integer> ajaxIntegerIds) {
		return authenticationRpcService.deleteManagerByIds(ajaxIntegerIds,SecurityUtils.getSubject().getPrincipal().toString());
	}

	@Override
	public void resetManagerPassword(String password, List<Integer> ids) {
		authenticationRpcService.resetManagerPassword(password,ids,SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
	}

	@Override
	public List<Role> getRoleList(Integer sessionAdminId) {
		
		
		
		return authenticationRpcService.getRoleList(sessionAdminId,SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
	}

	@Override
	public int allocateUserRole(Integer userId, List<UserRole> list) {
		
		authenticationRpcService.allocateUserRole(userId,list,SecurityUtils.getSubject().getPrincipal().toString(),ConfigUtils.getProperty("app.code"));
		
		return 0;
	}
}
